package com.acoffee.springcloud.auth.controller;

import com.acoffee.springcloud.common.entity.ResponseEntity;
import com.acoffee.springcloud.common.utils.JWTUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * @author acoffee
 * @create 2022-02-11 10:18
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    @RequestMapping("/login")
    public ResponseEntity<String> login(String uname, String pwd, HttpServletResponse response){
        if ("aaa".equals(uname)&&"aaa".equals(pwd)){
            String token = JWTUtils.create(uname);
            System.out.println(token);
            //将token设置在响应头中
            response.setHeader("token",token);
            //前后端分离项目，必须要暴露响应头，前端才能获取
            response.setHeader("Access-Control-Expose-Headers","token");
            return new ResponseEntity<>(200,"ok","login success");
        }
        return new ResponseEntity<>(401,"not auth","login error");
    }
}
